package itbour.onetouchshow.widget.inching;

import android.content.Context;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;

import itbour.onetouchshow.R;
import itbour.onetouchshow.bean.canvas.CompoundPathsBean;
import itbour.onetouchshow.bean.canvas.PosBean;
import itbour.onetouchshow.bean.inching.noppt.InchingOutLineAllTypeV10Bean;
import itbour.onetouchshow.bean.inching.noppt.InchingOutLineImgBean;
import itbour.onetouchshow.bean.inching.noppt.InchingOutLineTextOrShapeBean;
import itbour.onetouchshow.singlecase.InchingDataSingleCase;
import itbour.onetouchshow.utils.CommonUtils;
import itbour.onetouchshow.utils.L_;
import itbour.onetouchshow.utils.UIUtils;


/**
 * Created by zjl_d on 2016/12/15.
 * 这是控制边框的控件
 */
public class VideoInchingFrontBoderView extends NewInchingFrontBoderView {

    private PosBean videoFrame;

    public VideoInchingFrontBoderView(Context context) {
        this(context, null);
    }

    public VideoInchingFrontBoderView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VideoInchingFrontBoderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setVideoEnabledErea(PosBean videoFrame) {
        this.videoFrame = videoFrame;
        //getEnabledEreaXY();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        getParent().requestDisallowInterceptTouchEvent(true);
        int ea = event.getAction();
        if (ea == MotionEvent.ACTION_DOWN) {
            super.onTouchEvent(event);
        } else if (ea == MotionEvent.ACTION_MOVE) {
            getNowSelectView().setThisDottedLineIsShow(0);
            actionMove(event);

            int[] locationMove = new int[2];
            getLocationOnScreen(locationMove);//获取在整个屏幕内的绝对坐标
            int viewInWindowsX = locationMove[0];
            int viewInWindowsY = locationMove[1];
            if (inController) {

                //如果在控制点中 变换边框的位置
                L_.i("inController");
                float newW = event.getRawX() - viewInWindowsX;
                float newH = event.getRawY() - viewInWindowsY;
                //最小的宽高
                int minWH = UIUtils.dp2px(10);
                int maxW = rlInchingBaseArea.getMaxW() - ((int) getX() - rlInchingBaseArea.getLeftZone());
                int maxH = rlInchingBaseArea.getMaxH() - ((int) getY() - rlInchingBaseArea.getTopZone());
                L_.i("ACTION_MOVEgetX===" + getX() + "getY===" + getY() + "maxW===" + maxW + "maxH===" + maxH + "减去X==" +
                        ((int) getX() - rlInchingBaseArea.getLeftZone()) + "减去Y===" + ((int) getY() - rlInchingBaseArea.getTopZone()));

                newW = newW < minWH ? minWH : newW;
                newH = newH < minWH ? minWH : newH;

                //计算最大的宽高
                newW = newW > maxW ? maxW : newW;
                newH = newH > maxH ? maxH : newH;
                //刷新边框控件
                refreshFontBoderView(newW, newH);


                InchingOutLineAllTypeV10Bean allTypeData = nowSelectView.getData();
                if (allTypeData instanceof InchingOutLineImgBean) {
                    InchingOutLineImgBean inchingOutLineImgBean = (InchingOutLineImgBean) allTypeData;
                    CompoundPathsBean compoundPathsBean = inchingOutLineImgBean.getMask().get(0);

                    InchingOperaRelativeLayout nowSelectView = getNowSelectView();
                    InchingOpreaImageView img = nowSelectView.findViewById(R.id.inching_can_opear_img);


                    //计算path的对于当前可见控件的缩放比及其偏移量
                    float wMask = compoundPathsBean.getW();
                    float hMask = compoundPathsBean.getH();


                    float maskScaleW = newW * 1.0f / wMask;
                    float maskScaleH = newH * 1.0f / hMask;


                    float maskX = 0.5f * (img.getWidth() - wMask * maskScaleW);
                    float maskY = 0.5f * (img.getHeight() - hMask * maskScaleH);

                    Path path = CommonUtils.getOnControlMoveScalePath(compoundPathsBean, maskScaleW, maskScaleH, maskX, maskY);


                    int thisInchingImageWidth = img.getWidth();
                    int thisInchingImageHeight = img.getHeight();

                    float sw = newW * 1.0f / thisInchingImageWidth;
                    float sh = newH * 1.0f / thisInchingImageHeight;
                    float scale = sw > sh ? sw : sh;

                    float ivW = scale * thisInchingImageWidth;
                    float ivh = scale * thisInchingImageHeight;
                    float ivX = 0.5f * (newW - ivW);
                    float ivY = 0.5f * (newH - ivh);

                    L_.i("图片测试===ivX===" + ivX + "ivY===" + ivY
                            + "ivW===" + ivW + "ivH===" + ivh + "scale==="
                            + scale + "newW===" + newW + "newH===" + newH);

                    img.refreshInchingImageViewMask(ivX, ivY);
                    img.setOnMaskChange(true, path, ivW, ivh);
                    img.setNeedClip(false);

                }


            } else {
                //如果不在控制点中 移动该控件
                L_.i("uninController");

                int dx = (int) event.getRawX() - lastX;
                int dy = (int) event.getRawY() - lastY;

                int l = getLeft() + dx;
                int b = getBottom() + dy;
                int r = getRight() + dx;
                int t = getTop() + dy;

                int minX = rlInchingBaseArea.getlB();
                int minY = rlInchingBaseArea.gettB();
                int maxX = rlInchingBaseArea.getrB();
                int maxY = rlInchingBaseArea.getbB();
                //移动时计算边界
                if (l < minX) {
                    l = minX;
                    r = l + getWidth();
                }
                if (t < minY) {
                    t = minY;
                    b = t + getHeight();
                }

                if (r > maxX) {
                    r = maxX;
                    l = r - getWidth();
                }

                if (b > maxY) {
                    b = maxY;
                    t = b - getHeight();
                }

                lastX = (int) event.getRawX();
                lastY = (int) event.getRawY();

                refreshFontBoderView(l, t);
                nowSelectView.moveThisView(l - rlInchingBaseArea.getLeftZone(), t - rlInchingBaseArea.getTopZone());

            }
        } else if (ea == MotionEvent.ACTION_UP) {
            long moveTime = System.currentTimeMillis() - currentMS;//移动时间
            //认为移动了
            if (moveTime > 50 && (moveX > 2 || moveY > 2)) {
                //都是抬手后更新微调数据
                int x = (int) (getX() - rlInchingBaseArea.getLeftZone());
                int y = (int) (getY() - rlInchingBaseArea.getTopZone());
                int w = getWidth() - rlInchingBaseArea.getCw() / 2;
                int h = getHeight() - rlInchingBaseArea.getCh() / 2;

                int by = y + h;
                int videoTopY = (int) (videoFrame.getY() * InchingDataSingleCase.INSTANCE.getScale());
                int viewBoundTopY = videoTopY - h;
                y = y > viewBoundTopY ? viewBoundTopY : y;
                if (by > videoTopY) {
                    by = videoTopY;
                    h = by - y;
                }
                L_.e("更新数据upDateThisViewx====" + x + "y===" + y + "w===" + w + "h===" + h + "getWidth===" + getWidth() + "getHeight===" + getHeight());
                /*是否是控制点控制*/
                InchingOutLineAllTypeV10Bean allTypeData = nowSelectView.getData();

                if (allTypeData instanceof InchingOutLineImgBean) {
                    InchingOutLineImgBean inchingOutLineImgBean = (InchingOutLineImgBean) allTypeData;
                    PosBean pos = inchingOutLineImgBean.getPos();
                    pos.setW(w);
                    pos.setH(h);
                    pos.setX(x);
                    pos.setY(y);
                    inchingOutLineImgBean.setPos(pos);
                    nowSelectView.setData(inchingOutLineImgBean);

                } else if (allTypeData instanceof InchingOutLineTextOrShapeBean) {
                    InchingOutLineTextOrShapeBean lineTextOrShapeBean = (InchingOutLineTextOrShapeBean) allTypeData;
                    PosBean pos = lineTextOrShapeBean.getPos();
                    pos.setW(w);
                    pos.setH(h);
                    pos.setX(x);
                    pos.setY(y);
                    lineTextOrShapeBean.setPos(pos);
                    nowSelectView.setData(lineTextOrShapeBean);
                }

                nowSelectView.upDateThisView(inController, nowPageIndex, x, y, w, h, this);
            } else {
                //执行点击事件
                performClick();
            }

        }
        return true;

    }


    /**
     * 得到视频不可编辑区域
     */
    private int[] getEnabledEreaXY() {
        int w = (int) videoFrame.getW();
        int h = (int) videoFrame.getH();
        int x = (int) videoFrame.getX();
        int y = (int) Math.ceil(videoFrame.getY());
        int videoH = (int) videoFrame.getH();
        int[] whd = UIUtils.WHD();
        int visableW = whd[0];
        float scale = w * 1.0f / visableW;
        y = (int) Math.ceil(y * 1.0f / scale);
        x = (int) (x * scale);
        h = (int) (visableW * h * 1.0f / w);
        videoH = (int) (videoH * 1.0f / scale);
        L_.e("不可编辑区域====" + x + "y===" + y + "w===" + w + "h===" + h + "videoH===" + videoH);
        return new int[]{x, y, h};

    }


}
